package com.iflytek.autofly.setting.widget;

import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.iflytek.autofly.setting.R;

/**
 * <b>Description: </b>用于left pannel的button<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/11/19</td>
 * <td>17:46</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class LeftPannelButton extends LinearLayout implements Checkable {

    public TextView mTextView;
    public RadioButton mRadioButton;
    private Context mContext;
    private boolean mChecked;
    private LinearGradient mCheckedTextColor;

    public LeftPannelButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    public LeftPannelButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        initView();
    }

    public LeftPannelButton(Context context) {
        super(context);

        mContext = context;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.left_pannel_button, this, false);
        mTextView = (TextView) view.findViewById(R.id.main_text);
        mRadioButton = (RadioButton) view.findViewById(R.id.radio_button);
        addView(view);
    }

    @Override
    public boolean isChecked() {
        return mRadioButton.isChecked();
    }

    @Override
    public void setChecked(boolean checked) {
        mChecked = checked;
        mRadioButton.setChecked(checked);
        if (!mChecked) {
            mTextView.getPaint().setShader(null);
        } else {
            if (mCheckedTextColor == null) {
                mCheckedTextColor = new LinearGradient(0, 0, 0, mTextView.getTextSize(),
                        getResources().getColor(R.color.main_left_button_font_color_pressed_start),
                        getResources().getColor(R.color.main_left_button_font_color_pressed_end),
                        Shader.TileMode.MIRROR);
            }
            mTextView.getPaint().setShader(mCheckedTextColor);
        }
        mTextView.invalidate();
    }

    @Override
    public void toggle() {
        mRadioButton.toggle();
    }

    public void setText(String text) {
        mTextView.setText(text);
    }

}
